﻿
namespace MefContrib.Models.Provider.Definitions.Configurable
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    
    public class PartCatalogConfigurationMetadataCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, object> AsDictionary()
        {
            Dictionary<string, object> metadata =
                new Dictionary<string, object>();
            
            foreach (PartCatalogConfigurationMetadata configurationMetadata in this)
            {
                metadata.Add(configurationMetadata.Name, configurationMetadata.Value);
            }

            return metadata;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new PartCatalogConfigurationMetadata();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        protected override object GetElementKey(ConfigurationElement element)
        {
            PartCatalogConfigurationMetadata metadata =
                element as PartCatalogConfigurationMetadata;

            if (metadata != null)
            {
                return metadata.Name;
            }

            throw new InvalidOperationException();
        }
    }
}